

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import java.awt.Component;
import javax.swing.Box;
import javax.swing.JSeparator;
import java.awt.Font;
import java.awt.Color;

public class VueNouvCompteSociete extends Vue {

	private Societe _societe ;
	
	private JPanel contentPane;
	private JTextField textFieldNumS;
	private JTextField textFieldSolde;
	private JTextField textFieldDecouvert;
	private JTextArea textAreaInfosComptes;
	
	private final ButtonGroup buttonGroup = new ButtonGroup();
	private JButton btnRechercher;
	private JButton btnAnnuler;
	private JButton btnEnregistrer;
	private JLabel labelAdresse;
	private JLabel lblNbComptes;
	private JLabel labelNbCompte;
	private JLabel lblNouveauCompte;
	private JRadioButton rdbtnPEL;
	private JRadioButton rdbtnCEL;
	private JRadioButton rdbtnCourant;
	private JButton btnAnnul2;
	private JLabel nomSocieteLabel;
	private JLabel lblSiren;
	private JLabel lblNumSiren;
	private JLabel lblNomMand;
	private JLabel lblPrenomMand;
	/**
	 * Create the frame.
	 */
	public VueNouvCompteSociete(Controleur controleur) {
		super(controleur);
		setTitle("Enregistrement d'un nouveau compte pour une société existante");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 650, 550);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblClient = new JLabel("Société :");
		lblClient.setBounds(10, 6, 90, 16);
		contentPane.add(lblClient);
		
		JLabel lblNom = new JLabel("Numero Siren :");
		lblNom.setBounds(88, 34, 101, 16);
		contentPane.add(lblNom);
		
		textFieldNumS = new JTextField();
		textFieldNumS.setBounds(235, 29, 182, 28);
		contentPane.add(textFieldNumS);
		textFieldNumS.setColumns(10);
		
		JLabel lblAdresse = new JLabel("Adresse :");
		lblAdresse.setBounds(88, 217, 61, 16);
		contentPane.add(lblAdresse);
		
		lblNbComptes = new JLabel("Nb de comptes :");
		lblNbComptes.setBounds(313, 217, 104, 16);
		contentPane.add(lblNbComptes);
		
		
		btnRechercher = new JButton("Rechercher");
		btnRechercher.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String numS = textFieldNumS.getText().trim().toLowerCase();
				// liaison de la vue avec l'objet observe
				setSociete(getControleur().rechSociete(numS));
			}
			});
		
		btnRechercher.setBounds(337, 79, 117, 29);
		contentPane.add(btnRechercher);
		
		btnAnnuler = new JButton("Annuler");
		btnAnnuler.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					getControleur().fermerVue(VueNouvCompteSociete.this);}
		});
		btnAnnuler.setBounds(180, 79, 117, 29);
		contentPane.add(btnAnnuler);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(115, 257, 462, 47);
		contentPane.add(scrollPane);
		
		textAreaInfosComptes = new JTextArea();
		textAreaInfosComptes.setEditable(false);
		scrollPane.setColumnHeaderView(textAreaInfosComptes);

		JLabel lblCompte = new JLabel("Mandataire :");
		lblCompte.setBounds(15, 126, 85, 16);
		contentPane.add(lblCompte);
		
		JLabel lblType = new JLabel("Type :");
		lblType.setBounds(72, 414, 61, 16);
		contentPane.add(lblType);
		
		rdbtnCourant = new JRadioButton("Courant");
		buttonGroup.add(rdbtnCourant);
		rdbtnCourant.setBounds(131, 411, 79, 23);
		contentPane.add(rdbtnCourant);
		
		rdbtnCEL = new JRadioButton("CEL");
		buttonGroup.add(rdbtnCEL);
		rdbtnCEL.setBounds(218, 411, 53, 23);
		contentPane.add(rdbtnCEL);
		
		rdbtnPEL = new JRadioButton("PEL");
		buttonGroup.add(rdbtnPEL);
		rdbtnPEL.setBounds(273, 411, 53, 23);
		contentPane.add(rdbtnPEL);
		
		JLabel lblSoldeInitial = new JLabel("Solde initial :");
		lblSoldeInitial.setBounds(72, 451, 104, 16);
		contentPane.add(lblSoldeInitial);
		
		textFieldSolde = new JTextField();
		textFieldSolde.setColumns(10);
		textFieldSolde.setBounds(192, 446, 134, 28);
		contentPane.add(textFieldSolde);
		
		btnEnregistrer = new JButton("Enregistrer");
		btnEnregistrer.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String s = textFieldSolde.getText().trim().toLowerCase();
				String type;
				Societe sos = getSociete();
				if (rdbtnCourant.isSelected()) {
					type = "Courant";}
				else if (rdbtnCEL.isSelected()) {
					type = "CEL";}
				else {
					type = "PEL";}
				String d = textFieldDecouvert.getText();
				getControleur().nouvCompteSociete(type, s, d, sos);
				}
		});
		btnEnregistrer.setBounds(338, 482, 117, 29);
		contentPane.add(btnEnregistrer);
		
		JLabel lblDecouvert = new JLabel("D\u00E9couvert autoris\u00E9 :");
		lblDecouvert.setBounds(354, 451, 141, 16);
		contentPane.add(lblDecouvert);
		
		textFieldDecouvert = new JTextField();
		textFieldDecouvert.setColumns(10);
		textFieldDecouvert.setBounds(498, 446, 101, 28);
		contentPane.add(textFieldDecouvert);
		
		JSeparator separator = new JSeparator();
		separator.setBounds(9, 119, 622, 2);
		contentPane.add(separator);
		
		JSeparator separator_1 = new JSeparator();
		separator_1.setBounds(9, 344, 622, 2);
		contentPane.add(separator_1);
		
		lblNouveauCompte = new JLabel("Nouveau Compte :");
		lblNouveauCompte.setBounds(12, 356, 124, 15);
		contentPane.add(lblNouveauCompte);
		
		labelAdresse = new JLabel("");
		labelAdresse.setFont(new Font("Dialog", Font.PLAIN, 12));
		labelAdresse.setBounds(161, 218, 61, 15);
		contentPane.add(labelAdresse);
		
		labelNbCompte = new JLabel("");
		labelNbCompte.setFont(new Font("Dialog", Font.PLAIN, 12));
		labelNbCompte.setBounds(429, 218, 61, 15);
		contentPane.add(labelNbCompte);
		
		JLabel lblListeDesComptes = new JLabel("Liste des Comptes");
		lblListeDesComptes.setFont(new Font("Dialog", Font.PLAIN, 10));
		lblListeDesComptes.setBounds(10, 271, 104, 15);
		contentPane.add(lblListeDesComptes);
		
		JLabel label = new JLabel("€");
		label.setBounds(617, 452, 17, 15);
		contentPane.add(label);
		
		btnAnnul2 = new JButton("Annuler");
		btnAnnul2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				getControleur().fermerVue(VueNouvCompteSociete.this);
			}
		});
		btnAnnul2.setBounds(202, 482, 117, 29);
		contentPane.add(btnAnnul2);
		
		nomSocieteLabel = new JLabel("");
		nomSocieteLabel.setForeground(Color.DARK_GRAY);
		nomSocieteLabel.setFont(new Font("Dialog", Font.BOLD, 13));
		nomSocieteLabel.setBounds(171, 124, 303, 28);
		contentPane.add(nomSocieteLabel);
		
		lblSiren = new JLabel("Num Siren :");
		lblSiren.setBounds(88, 179, 79, 15);
		contentPane.add(lblSiren);
		
		lblNumSiren = new JLabel("");
		lblNumSiren.setBounds(171, 179, 61, 15);
		contentPane.add(lblNumSiren);
		
		JLabel lblNom_1 = new JLabel("Nom :");
		lblNom_1.setBounds(72, 152, 61, 15);
		contentPane.add(lblNom_1);
		
		JLabel lblPrenom = new JLabel("Prenom :");
		lblPrenom.setBounds(236, 152, 61, 15);
		contentPane.add(lblPrenom);
		
		lblNomMand = new JLabel("");
		lblNomMand.setBounds(115, 152, 61, 15);
		contentPane.add(lblNomMand);
		
		lblPrenomMand = new JLabel("");
		lblPrenomMand.setBounds(328, 152, 61, 15);
		contentPane.add(lblPrenomMand);
		
		JLabel lblSocit = new JLabel("Société :");
		lblSocit.setBounds(10, 179, 61, 15);
		contentPane.add(lblSocit);
		
	}

	/**
	 * definition des etats de la fenetre
	 */
	public void setEtat (int etat){
		switch (etat) {
		case initiale: {
			textFieldSolde.setEnabled(false);
			textFieldDecouvert.setEnabled(false);
			
			btnEnregistrer.setEnabled(false);
			textFieldSolde.setEditable(false);
			rdbtnCourant.setEnabled(false);
			rdbtnCEL.setEnabled(false);
			rdbtnPEL.setEnabled(false);
			textFieldDecouvert.setEditable(false);
			btnAnnul2.setEnabled(false);
			break;
			}
		case finale: {
			btnAnnul2.setEnabled(true);
			textFieldDecouvert.setEditable(true);
			rdbtnCourant.setEnabled(true);
			rdbtnCEL.setEnabled(true);
			rdbtnPEL.setEnabled(true);
			textFieldSolde.setEditable(true);
			btnAnnuler.setEnabled(false);
			btnRechercher.setEnabled(false);
			textFieldDecouvert.setEnabled(true);
			textFieldSolde.setEnabled(true);
			btnEnregistrer.setVisible(true);
			btnEnregistrer.setEnabled(true);
			break;
			}
		}
	}
	
	private Societe getSociete() {
		return _societe;
	}
	private void setSociete(Societe societe) {
		 _societe = societe;
	}
	
	public void alimente(Societe societe) {
		lblNomMand.setText(societe.getNomPdg());
		lblPrenomMand.setText(societe.getPrenomPdg());
		labelAdresse.setText(societe.getAdresse());
		labelNbCompte.setText(String.valueOf(societe.getNbComptes()));
		textAreaInfosComptes.setText("");
		for (CompteSociete cp : societe.getComptes()) {
			  textAreaInfosComptes.append( "Numéro " + String.valueOf(cp.getNumero()) + " solde " + String.valueOf(cp.getSolde()) + " type " + cp.getType() + "\n");
		}
	}
	
}
